别名
发表于 2018-3-6 09:28:35 | 分类于 SQL |
别名
通过使用 SQL,可以为表名称或列名称指定别名。
基本上,创建别名是为了让列名称的可读性更强。
列的 SQL 别名语法
SELECT column_name AS alias_name
FROM table_name;
表的 SQL 别名语法
SELECT column_name(s)
FROM table_name AS alias_name;
示例
示例使用样本数据库。
该库包含 "Websites" 表:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
下面是 "access_log" 网站访问记录表的数据:
mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
列的别名示例
指定两个别名,一个是 name 列的别名,一个是 country 列的别名。
提示:如果列名称包含空格,要求使用双引号或方括号:
SELECT name AS n, country AS c
FROM Websites;
执行输出结果:
mysql> SELECT name AS n, country AS c
-> FROM Websites;
+---------------+---------+
| n | c |
+---------------+---------+
| Google | USA |
| 淘宝 | CN |
| 菜鸟教程 | CN |
| 微博 | CN |
| Facebook | USA |
| stackoverflow | IND |
+---------------+--------+
把三个列(url、alexa 和 country)结合在一起,并创建一个名为 "site_info" 的别名:
SELECT name, CONCAT(url, ', ', alexa, ', ', country) AS site_info
FROM Websites;
执行输出结果:
mysql> SELECT name, CONCAT(url, ', ', alexa, ', ', country) AS site_info
-> FROM Websites;
+----+--------------+-----------------------------------------------+
| id | name | site_info |
+----+--------------+-----------------------------------------------+
| 1 | Google | https://www.google.cm/ , 1 , USA |
| 2 | 淘宝 | https://www.taobao.com/ , 13 , CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ , 4689 , CN |
| 4 | 微博 | http://weibo.com/ , 20 , CN |
| 5 | Facebook | https://www.facebook.com/ , 3 , USA |
| 7 | stackoverflow | http://stackoverflow.com/ , 0 , IND |
+----+---------------+------------------------------------ ---------+
###表的别名实例
选取 "菜鸟教程" 的所访问记录。使用 "Websites" 和 "access_log" 表,并分别为它们指定表别名 "w" 和 "a"(通过使用别名让 SQL 更简短):
SELECT w.name, w.url, a.count, a.date
FROM Websites AS w, access_log AS a
WHERE a.site_id=w.id and w.name="菜鸟教程";
执行输出结果:
mysql> SELECT w.name, w.url, a.count, a.date
-> FROM Websites AS w, access_log AS a
-> WHERE a.site_id=w.id and w.name="菜鸟教程";
+---------+---------------------------+-------+------------+
| name | url | count | date |
+---------+--------------------------+-------+------------+
| 菜鸟教程 | http://www.runoob.com/ | 100 | 2016-05-13 |
| 菜鸟教程 | http://www.runoob.com/ | 220 | 2016-05-15 |
| 菜鸟教程 | http://www.runoob.com/ | 201 | 2016-05-17 |
+---------+--------------------------+-------+------------+
不带别名的相同的 SQL 语句:
SELECT Websites.name, Websites.url, access_log.count, access_log.date
FROM Websites, access_log
WHERE Websites.id=access_log.site_id and Websites.name="菜鸟教程";
执行输出结果:
mysql> SELECT Websites.name, Websites.url, access_log.count, access_log.date
-> FROM Websites, access_log
-> WHERE Websites.id=access_log.site_id and Websites.name="菜鸟教程";
+---------+---------------------------+-------+------------+
| name | url | count | date |
+---------+--------------------------+-------+------------+
| 菜鸟教程 | http://www.runoob.com/ | 100 | 2016-05-13 |
| 菜鸟教程 | http://www.runoob.com/ | 220 | 2016-05-15 |
| 菜鸟教程 | http://www.runoob.com/ | 201 | 2016-05-17 |
+---------+--------------------------+-------+------------+
在下面的情况下,使用别名很有用:
- 在查询中涉及超过一个表
- 在查询中使用了函数
- 列名称很长或者可读性差
- 需要把两个列或者多个列结合在一起